package com.such.kit.database.primarykey.impl;

import java.sql.SQLException;
import javax.sql.DataSource;
import com.such.kit.database.JdbcCommons;
import com.such.kit.database.primarykey.PrimaryKeyGenerator;

/**
 * @作者 SUCH
 * @日期 2017-7-10 下午4:52:18
 * @描述 序列主键生成器
 */
public class SequencePKGenerator implements PrimaryKeyGenerator {

	private JdbcCommons jdbcCommons;
	private String sequenceName;

	public SequencePKGenerator(DataSource dataSource, String sequenceName) 
			throws SQLException, ClassNotFoundException {
		this.jdbcCommons = new JdbcCommons(dataSource);
		this.sequenceName = sequenceName;
	}

	public String pkValue() {
		long value = this.jdbcCommons.queryForSequenceNextValue(this.sequenceName);
		return String.valueOf(value);
	}

	public String pkField(String alias) {
		return this.jdbcCommons.getDialectCommons().SQL.buildSequenceNextValueSql(this.sequenceName, alias);
	}

}
